package com.chris.homecare.screens
{
	import com.chris.homecare.components.BreadcrumbComponenet;
	import com.chris.homecare.components.BulletHeadingComp;
	import com.chris.homecare.components.HeaderComponent;
	import com.chris.homecare.components.renderers.UpdatesListRenderer;
	import com.chris.homecare.data.DataUtil;
	import com.chris.homecare.data.FetchRemoteData;
	import com.chris.homecare.data.Update;
	import com.chris.homecare.utils.CommonUtils;
	import com.chris.homecare.utils.ScreenNames;
	
	import feathers.controls.List;
	import feathers.controls.Screen;
	import feathers.controls.renderers.IListItemRenderer;
	import feathers.data.ListCollection;
	import feathers.layout.VerticalLayout;
	
	import starling.display.Quad;
	import starling.events.Event;
	
	[Event(name="triggerUpdateScreen",type="starling.events.Event")]
	
	public class UpdatesListScreen extends Screen
	{
		public static const TRIGGER_UPDATE_SCREEN:String = "triggerUpdateScreen";
		
		private var headerComp:HeaderComponent;
		private var breadcrumbComp:BreadcrumbComponenet;
		private var updatesList:List;
		
		private var updatesCollection:ListCollection;
		private var bulletHeadingComp:BulletHeadingComp;
		
		public function UpdatesListScreen()
		{
			super();
		}
		
		override protected function initialize():void
		{
			CommonUtils.previousScreen = ScreenNames.MENU_SCREEN;
			
			var layout:VerticalLayout = new VerticalLayout();
			this.layout = layout;
			
			headerComp = new HeaderComponent();
			headerComp.addEventListener(Event.COMPLETE, onMenuBtnTrigger);
			addChild(headerComp);
			
			breadcrumbComp = new BreadcrumbComponenet("Updates");
			addChild(breadcrumbComp);
			
			bulletHeadingComp = new BulletHeadingComp("Updates List");
			addChild(bulletHeadingComp);
			
			updatesList = new List();
			updatesList.paddingTop = 10;
			updatesList.dataProvider = DataUtil.getAllUpdates();
			updatesList.addEventListener(Event.CHANGE, onUpdatesListChange);
			updatesList.itemRendererFactory = function ():IListItemRenderer
			{
				var renderer:UpdatesListRenderer = new UpdatesListRenderer();
				return renderer;
			};
			addChild(updatesList);
			updatesList.backgroundSkin = new Quad(10,10,0xFFFFFF);
			updatesList.backgroundDisabledSkin = new Quad(10,10,0xFFFFFF);
			
			super.initialize();
		}
		
		override protected function draw():void
		{
			var availHeight:int = stage.stageHeight - (breadcrumbComp.y + breadcrumbComp.height);
			updatesList.width = stage.stageWidth;
			updatesList.height = availHeight;
			
			super.draw();
		}
		
		private function onMenuBtnTrigger(event:Event):void
		{
			this.dispatchEventWith(Event.COMPLETE);
		}
		
		private function onUpdatesListChange(event:Event):void
		{
			FetchRemoteData.choosenUpdateId = Update(updatesList.selectedItem).id;
			this.dispatchEventWith(TRIGGER_UPDATE_SCREEN);
		}
		
	}
}